package com.ruoyi.business.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.business.domain.TOrderInfo;
import com.ruoyi.business.domain.TRecipientInfo;
import com.ruoyi.business.response.ResponseData;
import com.ruoyi.business.service.BasePayment;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.HttpResponse;
import com.ruoyi.common.utils.HttpUtils;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.HashMap;

@Service("lsPayService")
public class LsPayServiceImpl extends BasePayment {


    @Override
    public JSONObject createPayOrder(TOrderInfo orderInfo) throws Exception {
        JSONObject jsonObject = new JSONObject();
        TRecipientInfo userRecipientInfo = userRecipientService.selectTRecipientInfoById(orderInfo.getRecipientId());
        HashMap<String, String> headerMap = new HashMap();
        HashMap<String, String> dataMap = new HashMap();
        String qrcode = userRecipientInfo.getReceiptQrcode();
        String qrcode_ = HttpUtils.getParam(qrcode,"qr");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        headerMap.put("User-Agent","Nebula AlipayDefined(nt:WIFI,ws:360|0|3.0) AliApp(AP/10.0.18.062203) AlipayClient/10.0.18.062203 Language/zh-Hans useStatusBar/true");

//
//        String url = "https://api.umvtech.com/api/payment/getUserName";
//        dataMap.put("user_code",userRecipientInfo.getPhoneNo());
//        HttpResponse priceHttpResponse = HttpUtils.httpFormPost(url,headerMap,"",dataMap,"kiss查询用户请求","");
//        JSONObject resultObj = JSONObject.parseObject(priceHttpResponse.getDocument().body().text());
//        int resultCode = resultObj.getInteger("status");
//        String priceId = "";
//        if(resultCode==1){
//
//
//
//        }
        //https://qr.leshuazf.com/pay/qrc.do?qr=SYTD200387629

        logger.info("qrcode："+qrcode_);
        String url ="https://hsyls.leshuazf.com/pay/qrc.do?auth_code="+orderInfo.getAliAuthCode()+"&qr="+qrcode_;
        logger.info("收银台HTMLurl："+url);

        HttpResponse httpResponse = HttpUtils.httpFormGet(url,headerMap,"","乐刷");
        logger.info("返回的HTML:"+httpResponse.getDocument().text());

        headerMap.put("Referer","https://hsyls.leshuazf.com/");

        url = "https://syt.leshuazf.com/scan-s0/qrcode/business/authority/get?qrcodeNum="+qrcode_+"&payChannel=2";
        logger.info("authorityURL："+url);

        httpResponse = HttpUtils.httpFormGet(url,headerMap,"","乐刷");
        logger.info("乐刷authorityURL返回:"+httpResponse.getBody());

        JSONObject resultObj = JSONObject.parseObject(httpResponse.getDocument().body().text());
        if(!resultObj.getString("error_code").equals("0")){
            jsonObject.put("code","100");
            jsonObject.put("msg","获取连接失败");
            logger.info("========修改收款账号为不可用===============");
            userRecipientInfo.setChannelDisabled(1);
            userRecipientService.updateTRecipientInfo(userRecipientInfo);
            return jsonObject;
        }
        String  data = resultObj.getString("data");
//        JSONObject payInfoObj = new JSONObject();
//        payInfoObj.put("CUST_ID",cust_id);
//        payInfoObj.put("adviseId",ad_id);
//        payInfoObj.put("qrcode",qrcode_);
//        orderInfo.setQrcodeTxt(payInfoObj.toJSONString());
//        orderInfoDao.update(orderInfo);

        dataMap = new HashMap();
        dataMap.put("t0",data);
        dataMap.put("qrcode",qrcode_);
        dataMap.put("amount",orderInfo.getMoney()+"");
        url = "https://hsyls.leshuazf.com/cgi-bin/qr/order.cgi";
        logger.info("下单URL："+url);
        StringBuilder buf = new StringBuilder("");
        HttpUtils.buildPayParams(buf, dataMap);
        String preStr = buf.toString();
        logger.info("请求参数："+preStr);

        headerMap.put("Referer","https://hsyls.leshuazf.com/pay/qrc.do?auth_code="+orderInfo.getAliAuthCode()+"&qr="+qrcode_);
        headerMap.put("cookie","yeahka_uid=; yeahka_openId="+orderInfo.getAliUserId());
        logger.info("请求参数Referer："+"https://hsyls.leshuazf.com/pay/qrc.do?auth_code="+orderInfo.getAliAuthCode()+"&qr="+qrcode_);
        logger.info("请求参数cookie："+"yeahka_uid=; yeahka_openId="+orderInfo.getAliUserId());

        String payHttpResponse = HttpUtils.sendPostRequest(url,preStr,headerMap);

        logger.info("乐刷请求返回的:"+payHttpResponse);

        JSONObject payInforesultObj = JSONObject.parseObject(payHttpResponse);
        if(payInforesultObj.getIntValue("error_code")!=0){
            jsonObject.put("code","100");
            jsonObject.put("msg","获取连接失败");
            logger.info("========修改收款账号为不可用===============");
            userRecipientInfo.setChannelDisabled(1);
            userRecipientService.updateTRecipientInfo(userRecipientInfo);
            return jsonObject;
        }
        JSONObject jspay_info = payInforesultObj.getJSONObject("jspay_info");
        String tradeNO = jspay_info.getString("tradeNO");
        JSONObject payInfoObj = new JSONObject();
        payInfoObj.put("appid",payInforesultObj.getString("appid"));
        payInfoObj.put("merchant_id",payInforesultObj.getString("merchant_id"));
        payInfoObj.put("qrcode",qrcode_);
        payInfoObj.put("order_id",payInforesultObj.getString("order_id"));
        payInfoObj.put("notify_key",payInforesultObj.getString("notify_key"));
        payInfoObj.put("third_order_id",payInforesultObj.getString("third_order_id"));
        orderInfo.setQrcodeTxt(payInfoObj.toJSONString());
        orderInfoService.updateTOrderInfo(orderInfo);

        jsonObject.put("payUrl",tradeNO);
        jsonObject.put("orderNo",payInforesultObj.getString("order_id"));
        jsonObject.put("code","200");
        return jsonObject;
    }


    public JSONObject createWxPayOrder(TOrderInfo orderInfo) throws ServiceException, UnsupportedEncodingException {
        return null;
    }

    @Override
    public ResponseData smartReissueOrder(TOrderInfo orderInfo) throws Exception {
        return new ResponseData();
    }


}
